using System.Collections.Generic;
using UnityEngine;

/// <summary> 道具类型枚举 </summary>
public enum ItemType{
    All,
    /// <summary> 材料 </summary>
    MATERIAL = 1,
    /// <summary> 消耗品 </summary>
    CONSUMABLES = 2,
    /// <summary> 碎片 </summary>
    FRAGMENT = 3
}


[System.Serializable]
public partial class ItemData
{
    [SerializeField] private int _ItemID; // 道具ID
    [SerializeField] private string _Name; // 道具名称ID
    [SerializeField] private string _Icon; // 道具Icon_ID
    [SerializeField] private int _Quality; // 道具品质
    [SerializeField] private ItemType _Type; // 道具类型
    [SerializeField] private int _SType; // 是否可以出售
    [SerializeField] private int _SPrice; // 售出价格
    [SerializeField] private int _UType; // 是否可以使用
    [SerializeField] private string _DropShow; // 掉落显示
    [SerializeField] private int _AddValue; // 增加货币
    [SerializeField] private int _DropType; // 掉落类型
    [SerializeField] private int _DropId; // 调用掉落库
    [SerializeField] private bool _IsPiece; // 是否碎片
    [SerializeField] private string _Desc; // 道具说明ID

    #region 获取和应用属性区域
    /// <summary> 道具ID </summary>
    public int ItemID { get { return _ItemID; }}

    /// <summary> 道具名称ID </summary>
    public string Name { get { return _Name; }}

    /// <summary> 道具Icon_ID </summary>
    public string Icon { get { return _Icon; }}

    /// <summary> 道具品质 </summary>
    public int Quality { get { return _Quality; }}

    /// <summary> 道具类型 </summary>
    public ItemType Type { get { return _Type; }}

    /// <summary> 是否可以出售 </summary>
    public int SType { get { return _SType; }}

    /// <summary> 售出价格 </summary>
    public int SPrice { get { return _SPrice; }}

    /// <summary> 是否可以使用 </summary>
    public int UType { get { return _UType; }}

    /// <summary> 掉落显示 </summary>
    public string DropShow { get { return _DropShow; }}

    /// <summary> 增加货币 </summary>
    public int AddValue { get { return _AddValue; }}

    /// <summary> 掉落类型 </summary>
    public int DropType { get { return _DropType; }}

    /// <summary> 调用掉落库 </summary>
    public int DropId { get { return _DropId; }}

    /// <summary> 是否碎片 </summary>
    public bool IsPiece { get { return _IsPiece; }}

    /// <summary> 道具说明ID </summary>
    public string Desc { get { return _Desc; }}


    /// <summary> 生成一个Data </summary>
    public static ItemData CreateData(
            int lItemID, // 道具ID
            string lName, // 道具名称ID
            string lIcon, // 道具Icon_ID
            int lQuality, // 道具品质
            ItemType lType, // 道具类型
            int lSType, // 是否可以出售
            int lSPrice, // 售出价格
            int lUType, // 是否可以使用
            string lDropShow, // 掉落显示
            int lAddValue, // 增加货币
            int lDropType, // 掉落类型
            int lDropId, // 调用掉落库
            bool lIsPiece, // 是否碎片
            string lDesc // 道具说明ID
        )
    {
        ItemData lItemData = new ItemData();
        lItemData._ItemID = lItemID;
        lItemData._Name = lName;
        lItemData._Icon = lIcon;
        lItemData._Quality = lQuality;
        lItemData._Type = lType;
        lItemData._SType = lSType;
        lItemData._SPrice = lSPrice;
        lItemData._UType = lUType;
        lItemData._DropShow = lDropShow;
        lItemData._AddValue = lAddValue;
        lItemData._DropType = lDropType;
        lItemData._DropId = lDropId;
        lItemData._IsPiece = lIsPiece;
        lItemData._Desc = lDesc;
        return lItemData;
    }
    #endregion
}

[System.Serializable]
public partial class ItemDataManager : ScriptableObject
{
    /// <summary> 索引是否初始化 </summary>
    public bool initializedDataMap = false;


    /// <summary> 数据信息 </summary>
    [SerializeField] private List<ItemData> _dataList = new List<ItemData>();


    /// <summary> 数据索引 </summary>
    private Dictionary<int, int> _dataMap;


    /// <summary> 数据索引 </summary>
    private Dictionary<int, int> DataMap
    {
        get
        {
            if (!initializedDataMap)
            {
                _dataMap = new Dictionary<int, int>();

                for (int i = 0, iMax = _dataList.Count; i < iMax; ++i)
                {
                    # if UNITY_EDITOR
                    if (_dataMap.ContainsKey(_dataList[i].ItemID))
                    {
                        Debug.LogError("[Error]IconDataManager::Init(). 配置ID有重复! id = " + _dataList[i].ItemID);
                    }
                    #endif

                    _dataMap[_dataList[i].ItemID] = i;
                }

                initializedDataMap = true;
            }

            return _dataMap;
        }
    }


    /// <summary> 获取所有数据 </summary>
    public List<ItemData> GetDataAll()
    {
        return _dataList;
    }


    /// <summary> 是否包含该ID数据 </summary>
    public bool Contains(int id)
    {
        return DataMap.ContainsKey(id);
    }


    /// <summary> 通过ID获取数据 </summary>
    public ItemData GetData(int id)
    {
        int index;
        if (null == DataMap || !DataMap.TryGetValue(id, out index))
        {
            Debug.LogError("[Error]ItemDataManager::GetData(). data is not exist! ID = " + id);
            return null;
        }

        return _dataList[index];
    }
}
